package com.ys.service.hr.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.base.TbUserJobLevelManage;
import com.ys.entity.flow.YspModel;
import com.ys.entity.hr.TbAttendance;
import com.ys.entity.sys.TsUserChange;
import com.ys.entity.sys.YsMenu;
import com.ys.mapper.hr.HrMapper;
import com.ys.mapper.hr.TbAttendanceMapper;
import com.ys.service.flow.YspTaskService;
import com.ys.service.hr.TbAttendanceService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.Map;

@Service("tbAttendanceService")
public class TbAttendanceServiceImpl implements TbAttendanceService {


	@Autowired
	private TbAttendanceMapper tbAttendanceMapper;
	@Autowired
	private YspTaskService yspTaskService;

	public static Long MENU_ID = 10241L;
	@Override
	public R search(Map<String, Object> params) throws Exception {
		return null;
	}
	/**
	 * 新增
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R add(Map<String, Object> params) throws Exception {
//		TbAttendance tbAttendance = JSON.parseObject(JSON.toJSONString(params), TbAttendance.class);
		if (!params.get("paidPersonalLeave").toString().matches("\\d+(\\.\\d{0,2})?")){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("带薪事假-其他（小时）需要是数值，两位小数点！");
		}
		if (!params.get("monthlyAsDay").toString().matches("\\d+(\\.\\d{0,2})?")){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("带薪事假-其他（小时）需要是数值，两位小数点！");
		}
		if (!params.get("dailyAsHour").toString().matches("\\d+(\\.\\d{0,2})?")){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("带薪事假-其他（小时）需要是数值，两位小数点！");
		}

		TbAttendance entity = JSON.parseObject(JSON.toJSONString(params), TbAttendance.class);
		entity.setType(params.get("type").toString());
		entity.setStandards(params.get("standards").toString());
		entity.setPaidPersonalLeave(new BigDecimal(params.get("paidPersonalLeave").toString()));
		entity.setMonthlyAsDay(new BigDecimal(params.get("monthlyAsDay").toString()));
		entity.setDailyAsHour(new BigDecimal(params.get("dailyAsHour").toString()));
		tbAttendanceMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	@Override
	@Transactional
	public R edit(Map<String, Object> params) throws Exception {
		TbAttendance entity = JSON.parseObject(JSON.toJSONString(params), TbAttendance.class);
		
		//参数判断
		if (!params.get("paidPersonalLeave").toString().matches("\\d+(\\.\\d{0,2})?")){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("带薪事假-其他（小时）需要是数值，两位小数点！");
		}
		if (!params.get("monthlyAsDay").toString().matches("\\d+(\\.\\d{0,2})?")){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("带薪事假-其他（小时）需要是数值，两位小数点！");
		}
		if (!params.get("dailyAsHour").toString().matches("\\d+(\\.\\d{0,2})?")){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("带薪事假-其他（小时）需要是数值，两位小数点！");
		}
		entity.setType(params.get("type").toString());
		entity.setStandards(params.get("standards").toString());
		entity.setPaidPersonalLeave(new BigDecimal(params.get("paidPersonalLeave").toString()));
		entity.setMonthlyAsDay(new BigDecimal(params.get("monthlyAsDay").toString()));
		entity.setDailyAsHour(new BigDecimal(params.get("dailyAsHour").toString()));

		if (entity.getStatus()==0) {
			int tbUserJobLevelManage = new TbUserJobLevelManage()
					.setAttendanceId(entity.getId())
					.setStatus(1)
					.count();
			if (tbUserJobLevelManage > 0) {

				return R.error("已使用，不可停用");
			}
		}
		tbAttendanceMapper.update(entity);
		return R.ok("操作成功");
	}


}
